package cn.itwen.star.graph.core.service.impl;

import cn.itwen.star.graph.comfyui.client.pojo.ComfyuiModel;
import cn.itwen.star.graph.core.service.FreemarkerService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;

@Service
public class FreemarkerServiceImpl implements FreemarkerService {

    @Autowired
    private Configuration configuration;

    /**
     * 封装comfyui提交任务的参数
     * @param model 模型数据
     * @return 模板渲染后的数据  json
     */
    @Override
    public String renderText2Image(ComfyuiModel model) throws IOException, TemplateException {
        //读取模板文件
        Template template = configuration.getTemplate("t2i.ftlh");
        // 模板渲染
        StringWriter stringWriter = new StringWriter();
        template.process(Map.of("config",model), stringWriter);
        return stringWriter.toString();
    }
}
